package com.joyoung.robot.util;

import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.support.annotation.IdRes;

public class GetValuesUtil {
    private static final String TAG = "ymm-" + GetValuesUtil.class.getSimpleName();

    /**
     * 根据key值，获取Manifest配置中Meta的value值
     *
     * @param context Context
     * @param metaKey metaKey
     * @return metaValue
     */
    public static String getMetaValue(Context context, String metaKey) {
        Bundle metaData = null;
        String apiKey = null;
        if (context == null || metaKey == null) {
            LogUtil.w(TAG, "Param is wrong!");
            return null;
        }
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(),
                    PackageManager.GET_META_DATA);
            if (null != ai) {
                metaData = ai.metaData;
            }
            if (null != metaData) {
                apiKey = metaData.getString(metaKey);
            }
        } catch (NameNotFoundException e) {
            LogUtil.w(TAG, e.getMessage(), e);
        }
        return apiKey;
    }

    /**
     * 根据资源名称获取资源ID
     *
     * @param context 上下文
     * @param ResName 资源名称
     * @param defType 资源类型：（id、string、drawable、array等）
     * @return int ResId
     */
    @IdRes
    public static int getResIdByName(Context context, String ResName, String defType) {
        return context.getResources().getIdentifier(ResName, defType, context.getPackageName());
    }
}
